package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Category;
import com.abaaba.project2.bean.CategoryExample;
import com.abaaba.project2.bean.OrderExample;
import com.abaaba.project2.bean.bo.CategoryCreateBo;
import com.abaaba.project2.bean.bo.CategoryUpdateBo;
import com.abaaba.project2.bean.vo.CategoryChildrenVo;
import com.abaaba.project2.bean.vo.CategoryCreateVo;
import com.abaaba.project2.bean.vo.CategoryLevelOneVo;
import com.abaaba.project2.bean.vo.CategoryVo;
import com.abaaba.project2.mapper.CategoryMapper;
import com.abaaba.project2.service.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> queryCategory() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categories) {
            if (category.getPid() == 0) {
                CategoryVo categoryVo = new CategoryVo(category.getId(), category.getName(), category.getKeywords(), category.getDesc(),
                        category.getIconUrl(), category.getPicUrl(), category.getLevel(), new ArrayList<CategoryChildrenVo>());
                categoryVos.add(categoryVo);
            }
        }

        for (Category category : categories) {
            if ("L2".equals(category.getLevel())) {
                for (CategoryVo categoryVo : categoryVos) {
                    if (category.getPid().equals(categoryVo.getId())) {
                        CategoryChildrenVo categoryChildrenVo = new CategoryChildrenVo(category.getId(), category.getName(), category.getKeywords(), category.getDesc(), category.getIconUrl(),
                                category.getPicUrl(), category.getLevel());
                        categoryVo.getChildren().add(categoryChildrenVo);
                    }
                }
            } else
                continue;
        }
        return categoryVos;
    }

    @Override
    public List<CategoryLevelOneVo> queryCategoryByLevel() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CategoryLevelOneVo> categoryVos = new ArrayList<>();
        for (Category category : categories) {
            CategoryLevelOneVo categoryLevelOneVo = new CategoryLevelOneVo(category.getId(), category.getName());
            categoryVos.add(categoryLevelOneVo);
        }
        return categoryVos;
    }

    @Override
    public CategoryCreateVo insertCategory(CategoryCreateBo categoryCreateBo) {
        Category category = new Category();
        category.setName(categoryCreateBo.getName());
        category.setDesc(categoryCreateBo.getDesc());
        category.setKeywords(categoryCreateBo.getKeywords());
        category.setPid(categoryCreateBo.getPid());
        category.setLevel(categoryCreateBo.getLevel());
        category.setIconUrl(categoryCreateBo.getIconUrl());
        category.setPicUrl(categoryCreateBo.getPicUrl());
        category.setAddTime(new Date(System.currentTimeMillis()));
        category.setUpdateTime(category.getAddTime());
        category.setDeleted(false);
        int insert = categoryMapper.insert(category);
        CategoryCreateVo categoryCreateVo = new CategoryCreateVo(category.getId(), category.getName(), category.getKeywords(), category.getDesc(),
                category.getPid(), category.getIconUrl(), category.getPicUrl(), category.getLevel(), category.getAddTime(), category.getUpdateTime());
        return categoryCreateVo;
    }

    @Override
    public int updateCategory(CategoryUpdateBo categoryCreateBo) {
        Category category = new Category();
        category.setId(categoryCreateBo.getId());
        category.setName(categoryCreateBo.getName());
        category.setKeywords(categoryCreateBo.getKeywords());
        category.setDesc(categoryCreateBo.getDesc());
        category.setIconUrl(categoryCreateBo.getIconUrl());
        category.setPicUrl(categoryCreateBo.getPicUrl());
        category.setLevel(categoryCreateBo.getLevel());
        category.setPid(categoryCreateBo.getPid());
        category.setUpdateTime(new Date(System.currentTimeMillis()));
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public int deleteCategory(Category category) {
        category.setDeleted(true);
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public Long countSearchCategoryAll() {
        CategoryExample categoryExample = new CategoryExample();
        // 构造查询条件
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        Long code = categoryMapper.countByExample(categoryExample);
        return code;
    }
}
